//
//  MovieBaseCollectionView.m
//  MyMovie
//
//  Created by apple on 16/5/18.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "MovieBaseCollectionView.h"

@interface MovieBaseCollectionView ()
{
    CGSize _cellSize;
}

@end

@implementation MovieBaseCollectionView

- (instancetype)initWithFrame:(CGRect)frame WithCellSize:(CGSize)cellSize {

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    _cellSize = cellSize;

    flowLayout.itemSize = cellSize;

    //计算第一个和最后一个视图的间隙
    CGFloat width = frame.size.width/2 - flowLayout.itemSize.width/2;

    //设置四周距离
    flowLayout.sectionInset = UIEdgeInsetsMake(5, width, 5, width);
    //设置水平方向的最小间隙
    flowLayout.minimumLineSpacing = _cellSize.width*0.05;
    //设置竖直方向的最小间隙
    flowLayout.minimumInteritemSpacing = 10;
    //设置单元格滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;


    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {

        self.backgroundColor = [UIColor blueColor];
        //隐藏水平滑动条
        self.showsHorizontalScrollIndicator = NO;
        self.bounces = NO;
        self.dataSource = self;
        self.delegate = self;

    }

    return self;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _movieArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    return nil;

}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    CGFloat xOffset = targetContentOffset->x;
    CGFloat width = _cellSize.width*1.05;
    CGFloat index = xOffset / width;
    NSInteger indexInteger = index + 0.5;

    CGFloat targetXOffset = indexInteger * width;
    
    targetContentOffset->x = targetXOffset;

    self.currentIndex = indexInteger;
}

@end
